package com.example.ble_measure_plugin

import androidx.annotation.NonNull
import cn.net.aicare.algorithmutil.AlgorithmUtil
//import cn.net.aicare.modulelibrary.module.utils.AicareBleConfig

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** BleMeasurePlugin */
class BleMeasurePlugin : FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private lateinit var channel: MethodChannel

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "ble_measure_plugin")
        channel.setMethodCallHandler(this)
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        when (call.method) {
            "getPlatformVersion" -> {
                result.success("Android ${android.os.Build.VERSION.RELEASE}")
            }
            "getBodyFatData" -> {
                val map = call.arguments as Map<String, Any>
                val type = map["type"] as Int
                val sex = map["sex"] as Int
                val age = map["age"] as Int
                val weight = map["weight"] as Double
                val height = map["height"] as Int
                val adc = map["adc"] as Int
                var bodyData = AlgorithmUtil.getBodyFatData(type!!, sex!!, age!!, weight!!.toDouble(), height!!, adc!!)
                var data = mapOf<String,Any>(
                    "bmi" to bodyData.bmi,
                    "bfr" to bodyData.bfr,
                    "sfr" to bodyData.sfr,
                    "uvi" to bodyData.uvi,
                    "rom" to bodyData.rom,
                    "bmr" to bodyData.bmr,
                    "bm" to bodyData.bm,
                    "vwc" to bodyData.vwc,
                    "bodyAge" to bodyData.bodyAge,
                    "pp" to bodyData.pp,
                )
                return result.success(data)
            }

            "getMoreFatData"->{
                val map = call.arguments as Map<String,Any>
                val sex = map["sex"] as Int
                val height = map["height"] as Int
                val weight = map["weight"] as Double
                val bfr = map["bfr"] as Double
                val rom = map["rom"] as Double
                val pp = map["pp"] as Double

                val moreData =  AicareBleConfig.getMoreFatData(sex,height, weight, bfr, rom, pp)

                var data = mapOf(
                    "standardWeight" to moreData.standardWeight,
                    "controlWeight" to moreData.controlWeight,
                    "fat" to moreData.fat,
                    "removeFatWeight" to moreData.removeFatWeight,
                    "muscleMass" to moreData.muscleMass,
                    "protein" to moreData.protein,
                )
                return result.success(data)
            }

            else -> {
                result.notImplemented()
            }
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }
}
